package com.music.controller.basic;

import com.alibaba.fastjson.JSONObject;
import com.music.dto.basic.UserDTO;
import com.music.dto.system.TokenDTO;
import com.music.dto.utils.OpResultDTO;
import com.music.dto.utils.TableReqDTO;
import com.music.dto.utils.TableRspDTO;
import com.music.service.basic.UserService;
import com.music.utils.HttpRequestReader;
import com.music.utils.JWTUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Slf4j
@RestController
@RequestMapping("/api/user")
public class UserController {
    @Resource
    private UserService userService;

    /**
     * @Description: 获取用户信息
     * @Author dy
     * @Date 2021/12/10
     */
    @RequestMapping(value = "/getByUserId", method = RequestMethod.POST)
    public OpResultDTO getByUserId(@RequestHeader(name = "Access-Token") String token, HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            Long userId;
            if (jsonObject!=null) {
                userId = jsonObject.getLong("userId");
            } else {
                userId = tokenDTO.getAccountId();
            }
            opResult.setResult(userService.getByUserId(userId));
        } catch (Exception e) {
            log.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 更新记录
     * @Author: SN
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public OpResultDTO update(@RequestBody UserDTO userDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(userService.update(userDTO).toString());
        } catch (Exception e) {
            log.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 重置密码
     * @Author: SN
     * @Date: 2019/01/31 11:02
     */
    @RequestMapping(value = "/resetPwd", method = RequestMethod.POST)
    public OpResultDTO resetPwd(@RequestBody UserDTO userDTO) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            opResult.setMessage(userService.resetPwd(userDTO.getUserId(), userDTO.getLoginPwd()).toString());
        } catch (Exception e) {
            log.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    @RequestMapping(value = "/changeImage", method = RequestMethod.POST)
    public OpResultDTO changeImage(@RequestHeader(name = "Access-Token") String token, HttpServletRequest request) {
        OpResultDTO opResult = new OpResultDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            JSONObject jsonObject = HttpRequestReader.getJsonObject(request);
            String imageUrl = jsonObject.getString("imageUrl");
            opResult.setResult(userService.changeImage(tokenDTO.getAccountId(),imageUrl));
        } catch (Exception e) {
            log.error(e.toString());
            opResult.setMessage("error");
        }
        return opResult;
    }

    /**
     * @Description: 获取数据列表
     * @Author dy
     * @Date 2021/12/29
     */
    @RequestMapping(value = "/query", method = RequestMethod.POST)
    public TableRspDTO list4Table(@RequestHeader(name = "Access-Token") String token, @RequestBody TableReqDTO tableReqDTO) {
        TableRspDTO tableRspDTO = new TableRspDTO();
        try {
            TokenDTO tokenDTO = JWTUtil.verifyToken(token);
            tableRspDTO = userService.list4Table(tableReqDTO);
        } catch (Exception e) {
            log.error(e.toString());
        }
        return tableRspDTO;
    }

}
